﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GPSTrackingAdminWebsiteManager
{
    public partial class ForgotPassword : Telerik.Web.UI.RadAjaxPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSendPassword_Click(object sender, EventArgs e)
        {

            try
            {
                GPSTrackingServerDB.Data.UsersQuery usersQuery = new GPSTrackingServerDB.Data.UsersQuery();
                usersQuery.AppendEquals(GPSTrackingServerDB.Entities.UsersColumn.Email, txtEmail.Text);

                GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.Users> userEntities = GPSTrackingServerDB.Data.DataRepository.UsersProvider.Find(usersQuery);

                if (userEntities.Count > 0)
                {
                    string Name = userEntities[0].Name + " " + userEntities[0].FirstName + " " + userEntities[0].LastName;
                    string Password = userEntities[0].Password;

                    string body = string.Empty;
                    using (StreamReader reader = new StreamReader(Server.MapPath("~/ForgotPasswordTemplate.html")))
                    {
                        body = reader.ReadToEnd();
                    }
                    body = body.Replace("{Password}", Password);
                    body = body.Replace("{Name}", Name);

                    GPSTrackingEmailManager.Manager.Send(txtEmail.Text, "Recuperación de Contraseña", body, true);

                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Success", "show_Success('" + Name+", su contraseña fue enviada a su correo." + "','Usuario error ...');", true);
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "NoFoundEmail", "show_Info('" + "No se encontro el email digitado en la base de datos" + "','Usuario error ...');", true);
                }
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowErrorCatch", "show_Error('" + ex.Message + "','Usuario error ...');", true);
            }

            
        }
    }
}